package yxy.game.pm2.chat.command.player;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Nickname;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PlayerMessage;

/** 玩家 获取一个随机名字 */
@MessageType(proto = Command.PlayerRandomName, classtype = PlayerMessage.PlayerRandomNameRequest.class)
public class CMD_PlayerRandomName extends Command<PlayerMessage.PlayerRandomNameRequest> {

	@Override
	public void execute(MessageSource messageSource, PlayerMessage.PlayerRandomNameRequest request) {
		PlayerMessage.PlayerRandomNameResponse.Builder response = PlayerMessage.PlayerRandomNameResponse.newBuilder();

		try {
			String name = Nickname.getInstance().randomNameNotReuse();
			response.setNickname(name);
			send(messageSource, response, Result.Success);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, PlayerMessage.PlayerRandomNameResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}